﻿@model DiscountModel
@inject AdminAreaSettings adminAreaSettings

@{
    if (!String.IsNullOrEmpty(Model.Id))
    {

<div class="panel panel-default">
    <vc:admin-widget widget-zone="discount_details_applied_to_products_top" additional-data="Model" />
    <div class="panel-body">
        <div id="products-grid"></div>
    </div>
    <div class="panel-footer">
        <button type="submit" id="btnAddNewProduct" onclick="javascript:OpenWindow('@(Url.Action("ProductAddPopup", "Discount", new { discountId = Model.Id, btnId = "btnRefreshProducts", formId = "discount-form" }))', 800, 800, true); return false;" class="k-button"><i class="fa fa-plus"></i>&nbsp;@T("Admin.Promotions.Discounts.AppliedToProducts.AddNew")</button>
        <input type="submit" id="btnRefreshProducts" style="display: none" />
    </div>
    <vc:admin-widget widget-zone="discount_details_applied_to_products_bottom" additional-data="Model" />
</div>

        <script>
            $(document).ready(function () {
                $("#products-grid").kendoGrid({
                    dataSource: {
                        transport: {
                            read: {
                                url: "@Html.Raw(Url.Action("ProductList", "Discount", new { discountId = Model.Id }))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            },
                            destroy: {
                                url: "@Html.Raw(Url.Action("ProductDelete", "Discount", new { discountId = Model.Id }))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            }
                        },
                        schema: {
                            data: "Data",
                            total: "Total",
                            errors: "Errors",
                            model: {
                                id: "ProductId",
                                fields: {
                                    ProductName: {editable: false, type: "string" },
                                    ProductId: {editable: false, type: "string" }
                                }
                            }
                        },
                        error: function(e) {
                            display_kendoui_grid_error(e);
                            // Cancel the changes
                            this.cancelChanges();
                        },
                        pageSize: @(adminAreaSettings.DefaultGridPageSize),
                        serverPaging: true,
                        serverFiltering: true,
                        serverSorting: true
                    },
                    pageable: {
                        refresh: true,
                        pageSizes: [@(adminAreaSettings.GridPageSizes)]
                    },
                    editable: {
                        confirmation: false,
                        mode: "inline"
                    },
                    scrollable: false,
                    columns: [{
                        field: "ProductName",
                        title: "@T("Admin.Promotions.Discounts.AppliedToProducts.Product")",
                        template: '<a class="k-link" href="@Url.Content("~/Admin/Product/Edit/")#=ProductId#">#=kendo.htmlEncode(ProductName)#</a>',
                    }, {
                        command: [{
                            name: "destroy",
                            text: "@T("Admin.Common.Delete")"
                        }],
                        width: 150
                    }]
                });
            });
        </script>

        <script type="text/javascript">
            $(document).ready(function () {
                $('#btnRefreshProducts').click(function () {
                    //refresh grid
                    var grid = $("#products-grid").data('kendoGrid');
                    grid.dataSource.read();

                    //return false to don't reload a page
                    return false;
                });
            });
        </script>

    }
    else
    {
        <div class="note note-info">
            @T("Admin.Promotions.Discounts.AppliedToProducts.SaveBeforeEdit")
        </div>
    }
}